var express = require("express");
var router = express.Router();
var { Mvs, Users, Prouser, Tiwen, Advise, Goods, Cart, Comment, Car } = require('../utils/model');
var { createToken, checkToken } = require("../utils/token")
var axios = require("axios");
const { log } = require("debug");
router.get('/test', (req, res) => {
    res.json({
        code: 200,
        msg: "测试接口 成功 successful"
    })
})

router.get('/huoqu', (req, res) => {
    var limit = req.query.limit * 1 || 0;
    Mvs.find({}, { _id: 0 })
        .limit(limit)
        .sort({ _id: -1 })
        .then(result => {
            res.json({
                code: 200,
                msg: "拉取数据成功",
                result
            })
        })
})

router.post("/login", (req, res) => {
    var body = req.body;
    var username = body.username;
    var password = body.password;
    Users.findOne({
        name: username,
    }, {})
        .then(result => {
            if (result) {
                if (password == result.password) {
                    res.json({
                        msg: "登录成功"
                    })
                } else {
                    res.json({
                        msg: "账号或密码错误"
                    })
                }
            } else {
                res.json({
                    msg: "账号或密码错误111"
                })
            }

        })
})

//新项目
//获取 crsftoken
router.get("/gettoken", (req, res) => {
    // var token = createToken();
    res.json({
        token: 'token',
        msg: "获取token成功",
        type: 1,
        code: 200
    })
})

//登录
router.post("/signin", (req, res) => {
    var body = req.body;
    Prouser.findOne({
        mobile: body.phone,
    }).then(result => {
        if (result) {
            if (result.password == body.password) {
                var token = createToken({
                    mobile: body.phone,
                    password: body.password,
                    roleid: result.roleid,
                })
                res.json({
                    code: 200,
                    type: 1,
                    msg: "登录成功",
                    token
                })
            } else {
                res.json({
                    code: 200,
                    type: 0,
                    msg: "登录失败，用户名和密码不匹配"
                })
            }
        } else {
            res.json({
                code: 200,
                type: 0,
                msg: "登录失败，用户名和密码不匹配"
            })
        }
    })
})

//获取个人信息 token

router.post("/getuserinfo", (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        Prouser.findOne({
            mobile: mobile
        }).then(result => {
            res.json({
                code: 200,
                msg: "获取信息成功",
                result: result,
                type: 1
            })
        })

    })
})

//添加用户
router.post("/adduser", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = req.body;
        var addroleid = body.roleid;//获取用户选择的权限等级
        if (addroleid > roleid) {//用token里的用户等级比较
            res.json({
                code: 403,
                type: 0,
                msg: "你的权限不够，不能添加此类用户"
            })
        } else {
            // 权限判断后，进行用户添加
            Prouser.findOne({
                mobile: body.mobile
            }).then(data => {
                if (data) {
                    res.json({
                        code: 402,
                        type: 0,
                        msg: "手机号码已存在，请重新输入"
                    })
                } else {
                    body.time = new Date();//此处给数据库中添加了一个注册时间属性
                    Prouser.insertMany(body)
                        .then(result => {
                            res.json({
                                code: 200,
                                type: 1,
                                result,
                                msg: "用户添加成功"
                            })
                        })
                }
            })
        }
    })
})


//用户列表操作
// 查询用户
router.post("/getuserlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Prouser.find({}, {})
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户列表成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 根据_id 删除用户
router.post("/deluser", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Prouser.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "用户删除成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 根据_id 修改用户
router.post("/updateuserone", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = { ...req.body, time: new Date() }
        // delete body[_id];
        Prouser.updateMany({
            _id: req.body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "用户修改成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


//提交体温记录
router.post('/addtiwen', (req, res) => {
    const {
        tiwen
    } = req.body;
    checkToken(req, res, ({ mobile, roleid }) => {
        Prouser.findOne({
            mobile,
        }).then(userinfo => {
            Tiwen.insertMany({
                time: new Date(),
                mobile,
                username: userinfo.username,
                tiwen,
                ishei: "0",
                is_yin: "1"
            }).then(data => {
                Tiwen.find({ mobile }, {})
                    .sort({ _id: -1 })
                    .then(result => {
                        res.json({
                            code: 200,
                            msg: "添加成功",
                            result,
                            type: 1
                        })
                    })
                    .catch(err => {
                        res.json({
                            code: 500,
                            msg: "服务器异常",
                            err,
                            type: 0
                        })
                    })
            })
        })
    })
})

//获取的体温记录
router.post("/mytiwenlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.find({
            mobile
        }, {})
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    result,
                    msg: "查询成功"
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    type: 0,
                    err,
                    msg: "服务端异常"
                })
            })
    })
})

// 获取我的体温记录
router.post("/mytiwenlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.find({
            mobile
        }, {})
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "查询成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 根据_id 删除体温记录
router.post("/delOneList", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 根据_id 修改体温记录
router.post("/updatetiwenone", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = { ...req.body, time: new Date() }
        // delete body[_id];
        Tiwen.updateMany({
            _id: req.body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 根据_id 修改是否检测 
router.post("/updateishei", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.updateMany({
            _id: req.body._id
        }, {
            $set: {
                ishei: req.body.ishei
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})



// 根据_id 修改 核酸结果 
router.post("/updateisyin", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.updateMany({
            _id: req.body._id
        }, {
            $set: {
                is_yin: req.body.is_yin
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 获取首页的 banner
router.post("/gethomebanner", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        res.json({
            msg: "获取数据成功",
            code: 200,
            type: 1,
            result: [//数据写在此处，拿去渲染
                {
                    imgUrl: "http://47.104.209.44/base/img/1.jpg",
                    text: "万众一心,抗击肺炎"
                },
                {
                    imgUrl: "http://47.104.209.44/base/img/2.jpg",
                    text: "爱满春来。武汉加油！中国加油！"
                },
                {
                    imgUrl: "http://47.104.209.44/base/img/3.jpg",
                    text: "全党动员防疫情，人人都是安全员"
                },
                {
                    imgUrl: "http://47.104.209.44/base/img/4.jpg",
                    text: "万众一心众志成城科学防治战胜新型肺炎！"
                }
            ]
        })
    })
})

//添加意见
router.post("/addadvise", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Prouser.findOne({
            mobile
        }).then(userinfo => {
            Advise.insertMany({
                time: new Date(),
                mobile,
                username: userinfo.username,
                reply: [],//用于获取回复的
                ...req.body//扩展运算符将前端传过来的表单展开，添加到数据库里
            }).then(result => {
                res.json({
                    type: 1,
                    code: 200,
                    msg: "添加成功",
                    result,
                })
            }).catch(err => {
                res.json({
                    type: 0,
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
        })
    })
})

//获取意见列表
router.post("/getadviselist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Advise.find({}, {})
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    result,
                    msg: "意见查询成功",
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//根据传来的_id删除特定的那条意见
router.post("/delAdviseOne", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Advise.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    msg: "删除意见成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    type: 0,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//根据传来的_id来查询特定的意见返给前端
//这个接口用了两次，修改意见和意见回复
router.post("/getadvisebyid", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {
            _id: req.body._id
        }
        Advise.findOne(obj, {})
            .then(result => {
                res.json({
                    code: 200,
                    msg: "查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//修改意见
router.post("/updateadvisebyid", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {//前端传来的修改依据
            _id: req.body._id
        }
        Advise.updateMany(obj, {
            $set: {
                time: new Date(),
                title: req.body.title,
                content: req.body.content,
                category: req.body.category
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    msg: "修改成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//提交意见回复
router.post("/addreply", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var { _id, username, tips } = req.body;//都是前端传来的关键信息
        Advise.findOne({
            _id
        }).then(data => {
            var reply = data.reply || [];//此处注意是先拿到数据库中的意见回复，要么是有接着push，要么就为空再push
            reply.push({//声明一个新的reply来装要放入的信息
                author: username,
                mobile,
                roleid,
                tips,
                time: new Date(),
                id: reply.length
            })
            Advise.updateMany({//这时才执行数据库更新
                _id,
            }, {
                $set: {
                    reply
                }
            }).then(result => {
                res.json({
                    code: 200,
                    msg: "回复成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
        })
    })
})

// 商品列表 
router.post("/goodlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {}
        var limit = req.body.limit || 0;
        Goods.find(obj, {})
            .limit(limit)
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "商品查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 商品分类去重
router.post("/goodtypelist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Goods.distinct("type")
            .then(result => {
                res.json({
                    code: 200,
                    msg: "商品分类查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//商品详情
router.post("/sousuo", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var _id = req.body.goodId;
        Goods.findOne({
            _id
        }).then(data => {
            res.json({
                code: 200,
                type: 1,
                result: data,
                msg: "商品详情查询成功",
            })
        })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//添加商品到我的购物车
router.post("/addtocart", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        const {
            goodId,
            count
        } = req.body;
        var body = req.body;
        console.log(mobile)
        Cart.findOne({//先查询
            goodId,
        })
            .then(data => {
                if (data) {//如果有相同的数据，就做数量的累加
                    // count 累加 ++ 
                    Cart.updateMany({
                        goodId
                    }, {
                        $set: {
                            time: new Date()
                        },
                        $inc: {
                            count: count * 1 //$inc更新数据，做累加
                        }
                    }).then(result => {
                        res.json({
                            code: 200,
                            msg: "添加商品成功",
                            result,
                            type: 1
                        })
                    })
                } else {// 没有相同数据，直接新增
                    body.time = new Date();
                    body.checked = false;
                    body.mobile = mobile;
                    console.log(body);
                    Cart.insertMany(body)
                        .then(result => {
                            res.json({
                                code: 200,
                                msg: "新增商品成功",
                                result,
                                type: 1
                            })
                        })
                }
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//根据电话号码，查询我的购物车信息
router.post("/getmycart", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Cart.find({
            mobile
        })
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "查询成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//删除
router.post("/delcartone", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Cart.remove({
            _id: req.body._id
        }).then(result => {
            res.json({
                code: 200,
                msg: "删除成功",
                result,
                type: 1
            })
        })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//修改数量
router.post("/changecartcount", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var { _id, count } = req.body;
        Cart.updateMany({
            _id
        }, {
            $set: {
                count: count * 1
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "数量更新成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 修改选中 意义在于之后的批量删除是以数据库中checked做依据
router.post("/changecartcheck", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var { _id, checked } = req.body;
        var obj = {}
        if (_id) {
            obj = { _id }
        }
        Cart.updateMany(obj, {
            $set: {
                checked: checked
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "更新成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 批量删除  判断选中 
router.post("/delcartcheck", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Cart.remove({
            mobile,
            checked: true//删除条件：数据库中checked的情况
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "批量删除成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//自己的项目
//登录
router.post("/logined", (req, res) => {
    var body = req.body;
    console.log(body.mobile);
    Prouser.findOne({//去数据库中查询
        mobile: body.mobile,
    }).then(result => {
        if (result) {
            if (result.password == body.password) {//再对比密码
                var token = createToken({
                    mobile: body.mobile,
                    password: body.password,
                    roleid: result.roleid,//后端设置好token后传给前端
                })
                res.json({
                    code: 200,
                    type: 1,
                    msg: "登录成功",
                    token
                })
            } else {//此处是有此用户，但密码错误
                res.json({
                    code: 200,
                    type: 0,
                    msg: "登录失败，用户名和密码不匹配1"
                })
            }
        } else {//此处是数据库中没有对应用户
            res.json({
                code: 200,
                type: 0,
                msg: "登录失败，用户名和密码不匹配2"
            })
        }
    })
})

//注册
router.post("/zhuce", (req, res) => {
    var body = req.body;
    Prouser.findOne({//去数据库中查询
        mobile: body.mobile,
    }).then(result => {
        if (result) {
            res.json({
                code: 200,
                type: 0,
                msg: "电话号码已存在，请输入别的号码"
            })
        } else {//此处是数据库中没有对应用户，执行数据库添加
            body.time = new Date();
            body.roleid = body.roleid || '1';
            Prouser.insertMany(body)
                .then(result => {
                    res.json({
                        code: 200,
                        msg: "注册成功",
                        result: result,
                        type: 1
                    })
                })
                .catch(err => {
                    res.json({
                        code: 500,
                        msg: "服务器异常,注册失败",
                        err,
                        type: 0
                    })
                })
        }
    })
})

//根据解密tokne中的手机号获取此用户的信息
router.post("/getuser", (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        Prouser.findOne({
            mobile: mobile
        }).then(result => {
            res.json({
                code: 200,
                msg: "获取信息成功",
                result: result,
                type: 1
            })
        })

    })
})

//首页轮播图
router.post("/getlunbo", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        res.json({
            msg: "获取数据成功",
            code: 200,
            type: 1,
            result: [//数据写在此处，拿去渲染
                {
                    imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599731669745&di=0c864cfb957b703baa21b1666e4a89ab&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F838ba61ea8d3fd1f6aa61157324e251f95ca5f2b.jpg",
                    text: "yes , my lead !"
                },
                {
                    imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599731862166&di=30dea75b65e2bfd8ff2d88b6dbea32db&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180113%2F821d347cc9ab4c1180ab128b38b1bb6c.gif",
                    text: "当你无法再拥有时，你唯一能做的就是不要再忘记!"
                },
                {
                    imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599732095269&di=9c2e081f1605d4f72093abe6e3e925d7&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F2a7ac0db6054b118fb9ddc21bb155f3799d048bc.jpg",
                    text: "为利威尔兵长献出心脏！！"
                },
                {
                    imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599732223403&di=00598b1fc916b640fec4bf335a882eca&imgtype=0&src=http%3A%2F%2Fp4.ssl.cdn.btime.com%2Ft01104a771557de8f41.gif",
                    text: "错的不是我，是这个世界！"
                }
            ]
        })
    })
})

//用户列表
router.post("/getuserlist", (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        Prouser.find()
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取信息成功",
                    result: result,
                    type: 1
                })
            })
    })
})

//删除用户
router.post("/deleteuser", (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        var body = req.body;
        Prouser.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result: result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常,删除失败",
                    err,
                    type: 0
                })
            })
    })
})

//获取卖座网电影列表
router.get("/filmlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        axios({
            url: "https://m.maizuo.com/gateway",
            method: "get",
            headers: {
                'X-Host': 'mall.film-ticket.film.list'
            },
            params: {
                cityId: 110100,
                pageNum: 1,
                pageSize: 20,
                type: 1,
                k: 487473,
            }
        }).then(result => {
            if (result.data.msg == "ok") {
                res.json({
                    msg: "获取电影数据成功",
                    code: 200,
                    result: result.data,
                    type: 1,
                })
            }
        }).catch(err => {
            console.log('sdsdds')
            console.log(err);
            res.json({
                code: 500,
                msg: "服务器异常...",
                type: 0,
                err,
            })
        })
    })
})

//获取卖座网单个电影数据
router.get("/detaillist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        axios({
            url: "https://m.maizuo.com/gateway?",
            method: "get",
            headers: {
                'X-Host': 'mall.film-ticket.film.info'
            },
            params: {
                filmId: req.query.filmId,
                k: 487473,
            }
        }).then(result => {
            if (result.data.msg == "ok") {
                res.json({
                    msg: "获取电影数据成功",
                    code: 200,
                    result: result.data,
                    type: 1,
                })
            }
        }).catch(err => {
            console.log('sdsdds')
            console.log(err);
            res.json({
                code: 500,
                msg: "服务器异常...",
                type: 0,
                err,
            })
        })
    })
})

//获取数据库电影列表
router.post('/getmovielist', (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Mvs.find()

            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    type: 1,
                    code: 200,
                    msg: "本地电影列表拉取成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    type: 0,
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//获取单个电影信息
router.post('/getmovieone', (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = req.body
        console.log(body.id);
        Mvs.findOne({
            id: body.id
        })
            .then(result => {
                console.log(result);
                res.json({
                    type: 1,
                    code: 200,
                    msg: "单个电影拉取成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    type: 0,
                    code: 500,
                    msg: "拉取失败",
                    err
                })
            })
    })
})

//添加电影票到我的购物车
router.post("/addtocar", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        const {
            filmId,
            count
        } = req.body;
        var body = req.body;
        console.log(mobile)
        Car.findOne({//先查询
            filmId,
        })
            .then(data => {
                if (data) {//如果有相同的数据，就做数量的累加
                    // count 累加 ++ 
                    Car.updateMany({
                        filmId
                    }, {
                        $set: {
                            time: new Date()
                        },
                        $inc: {
                            count: count * 1 //$inc更新数据，做累加
                        }
                    }).then(result => {
                        res.json({
                            code: 200,
                            msg: "添加电影票成功",
                            result,
                            type: 1
                        })
                    })
                } else {// 没有相同数据，直接新增
                    body.time = new Date();
                    body.checked = false;
                    body.mobile = mobile;
                    console.log(body);
                    Car.insertMany(body)
                        .then(result => {
                            res.json({
                                code: 200,
                                msg: "新增电影票成功",
                                result,
                                type: 1
                            })
                        })
                }
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常000",
                    err,
                    type: 0
                })
            })
    })
})

//查询自己的购物车
router.post("/getmycar", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        const body = req.body;
        Car.find({
            mobile: body.mobile
        })
            .then(result => {
                res.json({
                    type: 1,
                    code: 200,
                    msg: "获取购物车成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    type: 0,
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//单删电影票
router.post("/delcarone", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        const body = req.body;
        Car.remove({
            filmId: body.filmId
        })
            .then(result => {
                res.json({
                    type: 1,
                    code: 200,
                    msg: "删除成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    type: 0,
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//单选修改选中状态
router.post("/changecarcheck", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var { filmId, checked } = req.body;
        var obj = {}
        if (filmId) {
            obj = { filmId }
        }

        Car.updateMany(obj, {
            $set: {
                checked: checked
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "更新成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//批量删除选中
router.post("/delcarcheck", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Car.remove({
            mobile,
            checked: true//删除条件：数据库中checked的情况
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "批量删除成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//修改票数
router.post("/changecarcount", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var { filmId, count } = req.body;
        Car.updateMany({
            filmId
        }, {
            $set: {
                count: count * 1
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "票数更新成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

//Comment评论类
//获取评论列表
router.post("/getplist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Comment.find()
            .then(result => {
                res.json({
                    type: 1,
                    code: 200,
                    msg: "获取评论成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    type: 0,
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//添加评论
router.post("/addpinglun", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        const body = req.body;
        Mvs.findOne({
            id: body.id,
        }).then(movie => {
            body.time = new Date(),
                body.mid = body.id;
            body.movie = movie;
            Comment.insertMany(body)
                .then(result => {
                    res.json({
                        type: 1,
                        code: 200,
                        msg: "获取评论成功",
                        result
                    })
                })
                .catch(err => {
                    res.json({
                        type: 0,
                        code: 500,
                        msg: "服务器异常",
                        err
                    })
                })
        })
    })
})

//添加评论回复
router.post("/addhf", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var {
            _id,
            subtitle,
            mingzi
        } = req.body;
        Comment.findOne({//先利用_id找到数据库中的评论回复，将新的回复push进去
            _id
        }, {})
            .then(data => {
                var answer = data.answer || [];
                answer.push({
                    subtitle,
                    id: answer.length,
                    author: mingzi,
                    time: new Date()
                });
                Comment.updateMany({//再去修改数据库中内容
                    _id
                }, {
                    $set: {
                        answer
                    }
                }).then(result => {
                    res.json({//此处是用ajxs所以需要返回一个json数据
                        code: 200,
                        msg: "回复评论成功",
                        result,
                        type: 1
                    })
                })
            })
    })
})

//删除单个评论
router.post("/deleteone", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = req.body;
        Comment.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    type: 1,
                    code: 200,
                    msg: "删除评论成功",
                    result
                })
            })
            .catch(err => {
                res.json({
                    type: 0,
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//获取我的评论列表
router.post("/getmyping", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var {
            author
        } = req.body;
        Comment.find({//先利用_id找到数据库中的评论回复，将新的回复push进去
            author
        }).then(result=>{
            res.json({
                type: 1,
                code: 200,
                msg: "获取我的评论成功",
                result
            })
        })
        .catch(err => {
            res.json({
                type: 0,
                code: 500,
                msg: "服务器异常",
                err
            })
        })
    })
})

// 上传头像
// multer
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {   // 头像存放的目录 
        cb(null, './public/avatar')    // . 文件路径
    },
    filename: function (req, file, cb) {  // 新的文件名 
        cb(null, Date.now()+"WH2004"+file.originalname);  //  originalname 原文件名 
    }
})

var upload  =  multer({ storage: storage }).any();  // 可以上传任何格式的图片

router.post("/uploadpic",upload,(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
    //    console.log(req.files);
    //    var avatar = req.files[0],path;
    //    res.json({
    //        code:200,
    //        msg:"头像上传成功",
    //        type:1,
    //        avatar
    //    })
        if(req.files){
            var avatar = req.files[0].path;  // 头像的服务器地址 
            Prouser.updateMany({
                mobile
            },{
                $set:{
                    avatar
                }
            }).then(result=>{
                res.json({
                    code:200,
                    msg:"头像上传成功",
                    type:1,
                    avatar
                })
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    type:0
                })
            })
            
        }else{
            res.json({
                code:200,
                msg:"头像上传失败",
                type:0
            })
        }
    })
})


module.exports = router;